<?php
/**
 * 玛丝菲尔 手动数据控件
 * marisfrolgcontroller.php
 * 参数解释
 * marisfrolgType 类型  1往期回顾  2欲望视界 3大家都在看 4趋势报告 5最新 6最热 7推荐标签 8明星地图
 * limit 数据最大值 （空表示不限）  
 * Create on 2015-06-16 16:50:14
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\channel\marisfrolg;

class MarisfrolgController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	private $pageCount = 4;
	public function __construct($updateTime = null,$controlId,$isPublish){

		$this->savaLog();
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$parameters = array();
		
		if($controlData['manual']){
			$marisfrolgType = $controlData['manual']['marisfrolgType']?$controlData['manual']['marisfrolgType']:1;
			
			if($marisfrolgType !=5 || $marisfrolgType !=6){
				foreach($controlData['auto'] as $data){
					$parameterValue = json_decode($data['ParameterValue']);
					if(!isset($parameterValue->title)) $parameterValue->title = '';
					if(!isset($parameterValue->desc)) $parameterValue->desc = '';
					array_push($parameters,$parameterValue);
				}
			}
			
			switch($marisfrolgType){
				case 1:
					$this->setToReview($controlData['manual'],$parameters);
					break;
				case 3:
					$this->setPeopleSee($controlData['manual'],$parameters);
					break;
				case 2:
				case 4:
					$this->assign('parameters',$parameters);
					break;
				case 5:
					$this->setNewList($controlData['manual'],$controlData['auto']);
					break;
				case 6:
					$this->setHotList($controlData['manual'],$controlData['auto']);
					break;
				case 7:
					$this->setTag($parameters);
					break;
				case 8:
					$this->assign('parameters',$parameters);
					break;
				default:
					break;
			}
		}

		$path = $this->generateHtmlPath(__CLASS__);
		$this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		// 解析内容 更换链接
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	/**
	 * 处理往期回顾
	 */
	private function setToReview($manualData,$autoData){
		
		/* $temp = array();
		for($i=1;$i<=25;$i++){
			array_push($temp,$i);
		} */
		$lastData = '';
		$finalArr = array();
		if($autoData){
			$count = count($autoData);
			$step = ceil($count/12);
			$finalArr = array();
			for($i=1;$i<=$step;$i++){
				$arr = array_slice($autoData, ($i-1)*12,12);
				$finalArr[$i] = $arr;
			}
			$lastData = array_pop($autoData);
		}
		$this->assign('finalArr',$finalArr);
		$this->assign('lastData',$lastData);
	}
	
	/**
	 * 处理最新资讯
	 */
	private function setNewList($manualData,$autoData){
		$currentData = array();
		if($autoData){
			$this->parseMultiData($autoData,$manualData);
			if($this->packageArr){
				$currentPage = $this->getParameter(self::$CURRENTPAGE);
				$this->packageArr = array_slice($this->packageArr, ($currentPage-1)*$this->pageCount,$this->pageCount);
				foreach($this->packageArr as $key=>$arr){
					$entity = $arr['Entity'];
					$entity->Channel = self::getArticleUrl($entity->PublishId,$entity->ArticleTag->TagId);
					array_push($currentData,$entity);
				}
				// DO : 计算分页
				$this->getPage($currentPage);
			}
		};
		if(!$currentData) throw new \haibao\cms\business\BusinessException('内容已经为空,测试用');
		$this->assign('dataType','new');
		$this->assign('currentData',$currentData);
	}
	
	/**
	 * 处理最热资讯
	 */
	private function setHotList($manualData,$autoData){
		$currentData = array();
		if($autoData){
			$this->parseMultiData($autoData,$manualData);
			if($this->packageArr){
				
				// DO : 对数据按照浏览量排序进行排序
				$finalArr = $this->getArticleProperty();
				$currentPage = $this->getParameter(self::$CURRENTPAGE);
				
				$finalArr = array_slice($finalArr, ($currentPage-1)*$this->pageCount,$this->pageCount);
				foreach($finalArr as $key=>$arr){
					$arr->Channel = self::getArticleUrl($arr->PublishId,$arr->ArticleTag->TagId);
					array_push($currentData,$arr);
				}
				
				// DO : 计算分页
				$this->getPage($currentPage);
			}
		}
		if(!$currentData) throw new \haibao\cms\business\BusinessException('内容已经为空,测试用');
		$this->assign('dataType','hot');
		$this->assign('currentData',$currentData);
	}
	/**
	 * 获取分页数据
	 */
	private function getPage($currentPage){
		$prePage = $currentPage - 1;
		$nextPage = $currentPage + 1;
		$totalPage = ceil($this->packageArrCount/$this->pageCount);
		
		if ($prePage < 1){
			$prePage = 1;
		}
		if ($nextPage > $totalPage){
			$nextPage = $totalPage;
		}
		
		$pageStart = $currentPage - $this->pageCount;
		$pageEnd = $currentPage + $this->pageCount + 1;
		if ($pageStart < 1){
			$pageEnd = 1 - $pageStart + $pageEnd;
			if ($pageEnd > $totalPage){
				$pageEnd = $totalPage;
			}
			$pageStart = 1;
		}
		if ($pageEnd > $totalPage){
			$pageStart = $pageStart - ($pageEnd - $totalPage);
			if ($pageStart < 1){
				$pageStart = 1;
			}
			$pageEnd = $totalPage;
		}
		
		$this->assign('currentPage',$currentPage);
		$this->assign('prePage',$prePage);
		$this->assign('nextPage',$nextPage);
		$this->assign('totalPage',$totalPage);
		$this->assign('pages',range($pageStart,$pageEnd));
		$this->assign('pageStart',$pageStart);
		$this->assign('pageEnd',$pageEnd);
		$this->assign('funcName','getmarisfrolgList');
	}
	
	/**
	 * 处理推荐标签
	 */
	private function setTag($autoData){

		$finalArr = array();
		if($autoData){
			$count = count($autoData);
			$step = ceil($count/5);
			$finalArr = array();
			for($i=1;$i<=$step;$i++){
				$arr = array_slice($autoData, ($i-1)*6,6);
				$finalArr[$i] = $arr;
			}
			//$lastData = array_pop($autoData);
		}
		$this->assign('finalArr',$finalArr);
	}
	
	/**
	 * 获取文章属性（浏览量/评论量）
	 */
	private function getArticleProperty(){
		$articleIdArr = array();
		foreach($this->packageArr as $arr){
			array_push($articleIdArr,$arr['Entity']->Id);
		}
		$articleProperties = \haibao\cms\business\Article::getClickCommentNumsByArticleIds($articleIdArr);
		$finalArr = $temp = array();
		foreach($this->packageArr as $key=>$arr){
			$temp[$arr['Entity']->Id] = $arr['Entity'];
		}
		foreach($articleProperties as $key=>$property){
			
			if(isset($temp[$key])){
				array_push($finalArr,$temp[$key]);
			}
		}
		$this->packageArrCount = count($finalArr);
		return $finalArr;
	}
	
	/**
	 * 处理大家都在看
	 */
	private function setPeopleSee($manualData,$autoData){
		if(count($autoData) == 0){
			
			$dataCondition['source'] = __CLASS__;
			$dataCondition['isProImage'] = true;
			$dataCondition['limit'] = 4;
			$dataCondition['isTag'] = false;
			$dataCondition['isImageColl'] = false;
			$dataCondition['isReturnTag'] = false;
			$dataCondition['isDate'] = true;
			$dataCondition['dateType'] = 2;
			$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
			list($articles,$articleProImage) = $this->getArticleControl(2,$dataCondition);
			$articleArr = array();
			foreach($articles as $article){

				$temp = array();
				$temp['title'] = $article->Title;
				$temp['desc'] = $article->Summary;
				$temp['href'] = self::getArticleUrl($article->PublishId,6);
				$temp['url'] = isset($articleProImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$articleProImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
            	(isset($articleProImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articleProImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
				array_push($articleArr,$temp);
			}
			$autoData = json_decode(json_encode($articleArr));
		}
		$this->assign('parameters',$autoData);
	}
}